package com.netty.modbus.dto.response;

import com.netty.modbus.exception.DataConvertException;
import com.netty.modbus.util.HexUtil;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

import java.nio.ByteOrder;

/**
 * description: ReadResponseBody <br>
 * @date: 2021/10/9 16:45 <br>
 * @author: lwy <br>
 * @version: 1.0 <br>
 */
public class ReadResponseBody extends ResponseBody {
    /**
     * 读的功能码
     * */
    private final byte[] functionCode={1,3};
    /**
     * 数据长度
     * */
    private final int dataLength;
    /**
     * 数据长度
     * */
    private final int allLength;

    /**
     * modbus数据内容
     * */
    private final byte[] data;
    /**
     * 数据内容校验
     * */
    private final byte[] modbusCrc16;
    /**整体校验*/
    private final byte[] crc16;



    public ReadResponseBody(byte[] data) {
        //获取数据长度，去除开始符（2），命令符（1），读取符（2）
        String lengthHex=HexUtil.byteToHex(data[5]);
        this.dataLength=HexUtil.HexStringToInt(lengthHex);
        this.allLength=data.length;
        //获取具体的数据内容
        this.data=new byte[dataLength];
        System.arraycopy(data,5,this.data,0,dataLength);
        this.modbusCrc16=new byte[2];
        System.arraycopy(data,8+dataLength,this.modbusCrc16,0,2);
        this.crc16=new byte[2];
        System.arraycopy(data,10+dataLength,this.crc16,0,2);
    }



    public int getAllLength() {
        return allLength;
    }

    public byte[] getData() {
        return data;
    }

    @Override
    public void decode(byte[] data) {

    }

    @Override
    public ByteBuf encode() throws DataConvertException {
        return null;
    }
}
